package com.egao.common.core.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 读取本地JSON文件内容
 * @author lym
 * @date 2024/8/23 15:23
 */
public class ReadJsonUtil {

    /**
     * 读取本地JSON文件内容
     * @param fileName 文件地址
     * @return String
     */
    public static String readJsonFromResource(String fileName) {
        InputStream is = ReadJsonUtil.class.getClassLoader().getResourceAsStream(fileName);
        if (is == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}
